package parttern.iterator;

import java.util.ArrayList;
import java.util.List;

/**
 * 迭代器模式
 *
 * @tips list/set/map/cursor
 */
public class CustomCollectImpl<T> implements CustomCollect<T> {
    private List<T> list = new ArrayList<T>();

    @Override
    public void add(T obj) {
        list.add(obj);
    }

    @Override
    public void remove(T obj) {
        list.remove(obj);
    }

    @Override
    public Iterator<T> iterator() {
        return new IteratorImpl<T>(list);
    }


    ///test
    public static void main(String[] args) {
        CustomCollectImpl<String> customCollect = new CustomCollectImpl<String>();
        customCollect.add("aa");
        customCollect.add("bb");
        customCollect.add("cc");
        customCollect.remove("bb");
        Iterator<String> iterator = customCollect.iterator();
        while (iterator.hasNext()) {
            System.out.println("iterator:" + iterator.next());
        }
    }
}
